package com.usian.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.usian.feign.ContentFeign;

import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("content")
public class ContentController {
    @Autowired
    private ContentFeign itemFeign;
    @RequestMapping("selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Integer id){
        List<TbContentCategory>list=itemFeign.selectContentCategoryByParentId(id);
        if (list.size()>0){
            return Result.ok(list);
        }
        return Result.error("查询失败");


    }

    @RequestMapping("insertContentCategory")
    public Result insertContentCategory(@RequestParam Integer parentId,@RequestParam String name){
        int num=itemFeign.insertContentCategory(parentId,name);
        if (num==2){
            return Result.ok();
        }
        return Result.error("添加失败");

    }
    @RequestMapping("deleteContentCategoryById")
    public Result deleteContentCategoryById(@RequestParam long categoryId){
        try {
            itemFeign.deleteContentCategoryById(categoryId);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("删除失败");
        }





    }
    @RequestMapping("updateContentCategory")
    public Result updateContentCategory(@RequestParam Integer id,@RequestParam String name){
        int num=itemFeign.updateContentCategory(id,name);
        if (num==1){
            return Result.ok();
        }
        return Result.error("修改失败");


    }
    @RequestMapping("selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(@RequestParam long categoryId){
        PageResult pageResult =itemFeign.selectTbContentAllByCategoryId(categoryId);
        if (pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
        return Result.error("查询失败");

    }

    @RequestMapping("insertTbContent")
    public Result insertTbContent(TbContent tbContent){
        int num=itemFeign.insertTbContent(tbContent);
        if (num==1){
            return Result.ok();
        }
        return Result.error("添加失败");
    }
    @RequestMapping("deleteContentByIds")
    public Result deleteContentByIds(@RequestParam long ids){
        int num=itemFeign.deleteContentByIds(ids);
        if (num==1){
            return Result.ok();
        }
        return Result.error("删除失败");
    }




}
